import 'package:fluent_ui/fluent_ui.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:uuid/uuid.dart';
import '../api/login.dart';
import '../controller/login_controller.dart';
import '../controller/system/user_controller.dart';
import '../init_context.dart';
import '../utils/view.dart';
import 'main_page.dart';

class Login extends StatefulWidget {
  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  final settingsController = ScrollController();
  final _uNameController = TextEditingController();
  final _pwdController = TextEditingController();
  final _codeController = TextEditingController();
  bool _showPassword = false;
  final _formKey = new GlobalKey<FormState>();
  final uuidFactory = Uuid();
  final loginController = Get.put(LoginController());
  final userController = Get.put(UserController());

  @override
  Widget build(BuildContext context) {
    loginController.captchaUrl = uuidFactory.v1();
    return Card(
        child: Container(
      padding: EdgeInsets.only(left: 200, right: 200, top: 0, bottom: 0),
      child: ScaffoldPage.scrollable(
          header: const PageHeader(leading: FlutterLogo(size: 70,),title: Text('登录'),commandBar: WindowButtons(),),
          children: [
            Form(
              key: _formKey,
              child: Column(
                children: [
                  TextFormBox(
                    header: '用户名',
                    placeholder: '请输入用户名',
                    controller: _uNameController,
                    style: TextStyle(height: 3.5),
                    validator: (text) {
                      if (text == null || text.isEmpty) return '用户名必填';

                      return null;
                    },
                    textInputAction: TextInputAction.next,
                  ),
                  const SizedBox(width: 10),
                  TextFormBox(
                    header: '密码',
                    placeholder: '请输入密码',
                    obscureText: !_showPassword,
                    maxLines: 1,
                    style: TextStyle(height: 3.5),
                    controller: _pwdController,
                    suffixMode: OverlayVisibilityMode.always,
                    textInputAction: TextInputAction.next,
                    validator: (text) {
                      if (text == null || text.isEmpty) return '密码必填';
                      return null;
                    },
                  ),
                  TextFormBox(
                    header: '验证码',
                    placeholder: '请输入验证码',
                    controller: _codeController,
                    validator: (text) {
                      if (text == null || text.isEmpty) return '验证码必填';
                      return null;
                    },
                    style: const TextStyle(height: 3.0),
                    textInputAction: TextInputAction.next,
                    suffix: GetBuilder<LoginController>(
                      builder: (_) => IconButton(
                        icon: Image.network(loginController.captchaUrl,
                            width: 100.0, height: 40.0),
                        onPressed: () {
                          loginController.captchaUrl = uuidFactory.v1();
                        },
                      ),
                    ) /**/,
                  ),
                  const SizedBox(width: 10),
                  Padding(
                    padding: EdgeInsets.only(top: 20, left: 30, right: 30),
                    child: FilledButton(
                        child: Container(
                          child: const Text('登录'),
                          width: 100,
                        ),
                        onPressed: () async {
                          if ((_formKey.currentState as FormState).validate()) {
                            LoginProvider l = Get.find<LoginProvider>();
                            Response res = await l.login({
                              "username": _uNameController.value.text,
                              "password": _pwdController.value.text,
                              "vcode": _codeController.value.text,
                              "uuid": loginController.uuid
                            });
                            final Map<String, dynamic> object =
                                decoder.convert(res.bodyString.toString());
                            if (object["code"] == 0) {
                              userController.setUserState(object);
                              userController.loginSucccess();
                              Navigator.pushNamedAndRemoveUntil(context,
                                  '/home', ModalRoute.withName('/home'));
                            } else {
                              showSnackbar(
                                context,
                                Snackbar(
                                  content: Text(object["msg"]),
                                ),
                              );
                            }
                          }
                        }),
                  )
                ],
              ),
            )
          ]),
    ));
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    settingsController.dispose();
    super.dispose();
  }

}
